/**
 * @Date 2025/1/22 16:28
 * @Created by 衡盛永
 */

package com.shangting.web.controller;

import com.shangting.common.config.MinioConfig;
import com.shangting.common.utils.MinioUtil;
import io.minio.messages.Bucket;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/admin/minio")
public class MinioController {
    @Resource
    private MinioUtil minioUtil;
    @Resource
    private MinioConfig minioConfig;

    // 判断桶是否存在
    @GetMapping("/bucketExists")
    public void bucketExists(@RequestParam("bucketName") String bucketName) {
        Boolean b = minioUtil.bucketExists(bucketName);
        System.out.println(bucketName + (b ? "存在" : "不存在"));
    }

    // 创建桶
    @GetMapping("/makeBucket")
    public void makeBucket(String bucketName) {
        Boolean b = minioUtil.makeBucket(bucketName);
        System.out.println(bucketName + (b ? "创建成功" : "创建失败"));
    }

    // 删除桶
    @GetMapping("/removeBucket")
    public void removeBucket(String bucketName) {
        Boolean b = minioUtil.removeBucket(bucketName);
        System.out.println(bucketName + (b ? "删除成功" : "删除失败"));
    }

    // 获取所有的桶
    @GetMapping("/getAllBuckets")
    public void getAllBuckets() {
        List<Bucket> allBuckets1 = minioUtil.getAllBuckets();
        System.out.println("全部的桶:" + allBuckets1);
    }

    // 文件上传返回url
    @PostMapping("/upload")
    public void upload(@RequestParam("file") MultipartFile file) {
        String objectName = minioUtil.upload(file);
        if (null != objectName) {
            System.out.println(minioConfig.getEndpoint() + "/" + minioConfig.getBucketName() + "/" + objectName);
        }
        System.out.println("文件上传返回url:" + objectName);
    }

    // 图片/视频预览
    @GetMapping("/preview")
    public void preview(@RequestParam("fileName") String fileName) {
        String preview = minioUtil.preview(fileName);
        System.out.println("图片/视频预览" + preview);
    }

    // 文件下载
    @GetMapping("/download")
    public ResponseEntity<byte[]> download(@RequestParam("fileName") String fileName, HttpServletResponse res) throws Exception {
        byte[] data = minioUtil.download(fileName);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
                .body(data);
    }

    // 根据url地址删除文件
    @PostMapping("/delete")
    public void remove(String url) {
        String objName = url.substring(url.lastIndexOf(minioConfig.getBucketName() + "/") + minioConfig.getBucketName().length() + 1);
        boolean remove = minioUtil.remove(objName);
        System.out.println(objName + (remove ? "删除成功" : "删除失败"));
    }
}
